<script>
import { useMpWxPosition } from '@/stores/mpWxPosition'
import { useCategory } from '@/stores/category'
import { useBanner } from '@/stores/banner'
let _this
export default {
  beforeCreate() {
    _this = this;
  },
  data() {
    return {
      mpWxPostion: {
        top: 0,
        height: 0,
        left: 0,
        right: 0,
        width: 0,
        bothSideMargin: 0,
        headerHeight:0,
        logoTop: 0,
        logoHeight: 0,
        logoWidth: 0,
        logoUrl: this.$api.getUrl() + '/static/images/lzdls.logo.png'
      }
    }
  },
  onLaunch: function() {
    // console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
    console.log('App Launch')
    console.log(uni.$uv.os())
    // console.log(uni.$uv.sys())
    console.log(uni.$uv.platform)
  },
  onShow: function() {
    console.log('App Show')
    // await _this.setMpWxPosition()
    // await _this.setMpUser()
    // await _this.setCategories()
    // await _this.setBanners()
    _this.setMpWxPosition()
    // _this.$api.mpLogin()
    _this.setCategories()
    _this.setBanners()
  },
  onHide: function() {
    console.log('App Hide')
  },
  methods: {
    setMpWxPosition() {
      console.log("setMpWxPosition")
      // 微信位置数据
      _this.mpWxPostion = Object.assign(_this.mpWxPostion, uni.getMenuButtonBoundingClientRect())
      let systemInfo = uni.getSystemInfoSync()
      _this.mpWxPostion.bothSideMargin = systemInfo.windowWidth - _this.mpWxPostion.right
      _this.mpWxPostion.headerHeight = _this.mpWxPostion.top + _this.mpWxPostion.height + 10
      _this.mpWxPostion.logoTop = 4;
      _this.mpWxPostion.logoHeight = _this.mpWxPostion.height - _this.mpWxPostion.logoTop * 2
      _this.mpWxPostion.logoWidth = _this.mpWxPostion.logoHeight * 324 / 76
      useMpWxPosition().updateMpWxPosition(_this.mpWxPostion)
    },
    setCategories() {
      console.log("setCategories")
      let updateCategoryList = () => {
        _this.$api.guestPost('/guest/pub/category-roots').then(res => {
          useCategory().updateCategoryList(res.data.list)
        })
      }
      if (useCategory().getCategory.list.length === 0) {
        updateCategoryList()
      }
      if (useCategory().getCategory.expiryTime < _this.$tool.getTimestamp()) {
        updateCategoryList()
      }
    },
    setBanners() {
      console.log("setBanners")
      let updateBannerList = () => {
        _this.$api.guestPost('/guest/pub/banner-list').then(res => {
          useBanner().updateBannerList(res.data.list)
        })
      }
      if (useBanner().getBanner.list.length === 0) {
        updateBannerList()
      }
      if (useBanner().getBanner.expiryTime < _this.$tool.getTimestamp()) {
        updateBannerList()
      }
    }
  }
}
</script>

<style lang="scss">
	/*每个页面公共css */
	//@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	//@import '@/static/customicons.css';
  /* iconfont */
  //@import "https://at.alicdn.com/t/c/font_3814524_b71c6o96iok.css";
	// 设置整个项目
	//.page {
	//	background-color: #f5f5f5;
	//}
  //.container {
  //  padding: 20px;
  //  font-size: 14px;
  //  line-height: 24px;
  //}
  //.mpWxPosition {}

	/* #endif */
	//.example-info {
	//	font-size: 14px;
	//	color: #333;
	//	padding: 10px;
	//}
</style>
